<?php

declare(strict_types=1);

namespace app\shop\model;
use app\seller\model\Category as CategoryModel;
use sheep\model\Common;

class Category extends Common
{
    protected $name = 'shop_category';

    // 追加属性
    protected $append = [
        'status_text',
        'type_text'
    ];



    /**
     * 默认状态列表
     *
     * @return array
     */
    public function typeList()
    {
        return [
            '1' => '经营类目',
            '2' => '经营资质类目',
        ];
    }


    public function getChildrenString($category)
    {
        $style = $category->style;
        $string = 'children';
        if (strpos($style, 'second') === 0) {
            $string .= '.children';
        } else if (strpos($style, 'third') === 0) {
            $string .= '.children.children';
        }

        return $string;
    }


    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id', 'id')->normal()->order('weigh', 'desc')->order('id', 'asc');
    }


    // 定义与父级分类的关联
    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    // 定义与子级分类的关联
    public function shopChildren()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }


    public function audit()
    {
        return $this->hasOne(CategoryModel::class, 'category_id');
    }

}
